package com.learning.spring.cloud.alibaba.ribbon.loadbalanced.web.controller;

import com.learning.spring.cloud.alibaba.ribbon.loadbalanced.web.model.User;
import com.learning.spring.cloud.alibaba.ribbon.loadbalanced.web.service.IUserService;
import com.learning.spring.cloud.alibaba.ribbon.loadbalanced.web.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * ClassName: UserController
 * Description:
 * Date: 2021/7/25 23:03 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * @param user
     * @return
     */
    @PostMapping("/save")
    public Integer insert(@RequestBody User user) {
        return userService.insert(user);
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Integer deleteByPrimaryKey(@PathVariable("id") Long id) {
        return userService.deleteByPrimaryKey(id);
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;


    /**
     * http://localhost:8001/user/query/1
     *
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    public UserVo selectByPrimaryKey(@PathVariable("id") Long id) {
        UserVo userVo = new UserVo();
        User user = userService.selectByPrimaryKey(id);

        ServiceInstance serviceInstance = this.loadBalancerClient.choose("01-ms-ribbon-order");

        // 打印信息：ServiceId:01-ms-ribbon-order; InstanceId:127.0.0.1:8002; Host:127.0.0.1; Scheme:null; Uri:http://127.0.0.1:8002; Port:8002; Secure:false; Metadata:{};
        log.info("ServiceId:{}; InstanceId:{}; Host:{}; Scheme:{}; Uri:{}; Port:{}; Secure:{}; Metadata:{}; ", serviceInstance.getServiceId(),
                serviceInstance.getInstanceId(),
                serviceInstance.getHost(),
                serviceInstance.getScheme(),
                serviceInstance.getUri(),
                serviceInstance.getPort(),
                serviceInstance.isSecure(),
                serviceInstance.getMetadata()
        );

        final List<String> forObject = restTemplate.getForObject(serviceInstance.getUri() + "/order/query/user/" + id, List.class);

        userVo.setId(user.getId());
        userVo.setName(user.getName());
        userVo.setOrderNameList(forObject);
        return userVo;
    }
}
